home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / InternetChooser.sit / Internet Chooser / main.c < prev    next >
C/C++ Source or Header  |  1996-06-22  |  9KB  |  372 lines

  1. #include "stdtypes.h"
  2. #include "stddebug.h"
  3.  
  4. #include "client.h"
  5. #include "datagram.h"
  6. #include "platform.h"
  7. #include "reggie.h"
  8. #include "socket.h"
  9.  
  10. #include "main.h"
  11.  
  12. // Globals
  13. Str255    gInfo = "\pHello World!";
  14. DialogPtr gChooserDialogPtr;
  15. Boolean   gQuitting;
  16. ListHandle gServiceList;
  17. ListHandle gURLList;
  18. short gFilterValue;
  19.  
  20. // * *******************************************************************************
  21. // * *******************************************************************************
  22.  
  23. void main(void) {
  24.     OSErr err = noErr;
  25.     
  26.     Initialize();
  27.     SetUpMenus();
  28.     if(SetUpChooser())
  29.         while(!gQuitting)
  30.             err = EventLoop();
  31.     DisposDialog(gChooserDialogPtr);
  32.     }
  33.     
  34. // * *******************************************************************************
  35. // * *******************************************************************************
  36.  
  37. OSErr SetUpMenus(void) {
  38.     InsertMenu(GetMenu(128), 0);
  39.     AddResMenu(GetMHandle(128), 'DRVR');
  40.     
  41.     InsertMenu(GetMenu(129), 0);
  42.     InsertMenu(GetMenu(130), 0);
  43.     
  44.     DisableItem(GetMenu(130), 1);
  45.     DisableItem(GetMenu(130), 2);
  46.     DisableItem(GetMenu(130), 3);
  47.     DisableItem(GetMenu(130), 4);
  48.     
  49.     DrawMenuBar();
  50.     }
  51.     
  52. // * *******************************************************************************
  53. // * *******************************************************************************
  54.  
  55. DialogPtr SetUpChooser(void) {
  56.     Rect ServiceRect, URLRect, InfoRect, ServiceListBounds, URLListBounds;
  57.     short iType, i=0, j=-1, row, total=0;
  58.     Handle iHandle, aServiceIcon;
  59.     Point serviceCellSize, URLCellSize;
  60.     Ptr cellData;
  61.     StringHandle iconString;
  62.     Cell currentCell;
  63.     OSErr err=noErr;
  64.     GrafPtr oldPort;
  65.     
  66.     GetPort(&oldPort);
  67.     
  68.     if(gChooserDialogPtr = GetNewDialog(128, NULL, (WindowPtr)-1)) {
  69.         SetPort(gChooserDialogPtr);
  70.         
  71.         GetDItem(gChooserDialogPtr, kServiceList, &iType, &iHandle, &ServiceRect);
  72.         GetDItem(gChooserDialogPtr, kURLList, &iType, &iHandle, &URLRect);        
  73.         
  74.         SetRect(&ServiceListBounds, 0, 0, 3, 3);
  75.         SetPt(&serviceCellSize, 60, 60);
  76.         gServiceList = LNew(&ServiceRect, &ServiceListBounds, serviceCellSize, 130, gChooserDialogPtr,
  77.             TRUE, FALSE, FALSE, TRUE);
  78.         
  79.         SetRect(&URLListBounds, 0, 0, 1, 10);
  80.         SetPt(&URLCellSize, 0, 0);
  81.         gURLList = LNew(&URLRect, &URLListBounds, URLCellSize, 0, gChooserDialogPtr,
  82.             TRUE, FALSE, FALSE, TRUE);
  83.         
  84.         
  85.         cellData = NewPtr(sizeof(char)*64);
  86.         if(MemError())
  87.             return(0);
  88.         i=128;
  89.         total = Count1Resources('STR ');
  90.         for(i=0; (i*3)+j<total; i++, j=0) {
  91.             for(j=0; (j<3) && ((i*3)+j<total);j++) {
  92.                 GetIconSuite(&aServiceIcon, (i*3)+j+128, svAllAvailableData);
  93.  
  94.                 iconString = GetString((i*3)+j+128);
  95.                 if(!iconString)
  96.                     break;
  97.                     
  98.                 SetPt((Point *)(¤tCell), j, i);
  99.                 BlockMove(&aServiceIcon, cellData, sizeof(Handle));
  100.                 BlockMove((*iconString)+1, cellData+sizeof(Handle), (*iconString)[0]);
  101.                 LSetCell(cellData, (*iconString)[0]+sizeof(Handle), currentCell, gServiceList);
  102.                 
  103.                 ReleaseResource((Handle)iconString);
  104.                 if(ResError())
  105.                     return(0);
  106.                 }
  107.             }
  108.         ShowWindow((WindowPtr)gChooserDialogPtr);
  109.         SetPort(oldPort);
  110.         }
  111.         
  112.     DisposePtr(cellData);
  113.     return(gChooserDialogPtr);
  114.     }
  115.     
  116. // * *******************************************************************************
  117. // * *******************************************************************************
  118.  
  119. //Initialize the ToolBox
  120. void Initialize(void) {
  121.     WindowPtr    mainPtr;
  122.     OSErr        error;
  123.     SysEnvRec    theWorld;
  124.         
  125.     gQuitting = FALSE;
  126.     
  127.     error = SysEnvirons(1, &theWorld);
  128.     if (theWorld.hasColorQD == false) {
  129.         SysBeep(50);
  130.         ExitToShell();                    /* If no color QD, we must leave. */
  131.         }
  132.     
  133.     /* Initialize all the needed managers. */
  134.     InitGraf(&qd.thePort);
  135.     InitFonts();
  136.     InitWindows();
  137.     InitMenus();
  138.     TEInit();
  139.     InitDialogs(nil);
  140.     InitCursor();
  141.     
  142.     MoreMasters();
  143.     MoreMasters();
  144.     MoreMasters();
  145.     }
  146.     
  147.  
  148. // * *******************************************************************************
  149. // * *******************************************************************************
  150.  
  151. void ChangeURLList(void) {
  152.     qThrowIfError(QueryTracker(0x12345678, 0, 100, NULL, NULL, NULL, 
  153.             "sils.umich.edu"), 0);
  154.     qThrowIfError(DumpRegEntries(), 0);
  155.     }
  156.  
  157. // * *******************************************************************************
  158. // * *******************************************************************************
  159.  
  160. void DoMouseInDialog(EventRecord theEvent, short theItem) {
  161.     GrafPtr oldPort;
  162.     Handle filterPopUp;
  163.     short iType;
  164.     Rect iRect;
  165.     RgnHandle infoRgn = NewRgn();
  166.     Point localPt;
  167.     
  168.     GetPort(&oldPort);
  169.     SetPort((GrafPtr)gChooserDialogPtr);
  170.  
  171.     localPt = theEvent.where;
  172.     GlobalToLocal(&localPt);
  173.     switch(theItem) {
  174.         case kServiceList:
  175.         case kServiceListScroll:
  176.             LClick(localPt, theEvent.modifiers, gServiceList);
  177.             ChangeURLList();
  178.             break;
  179.         case kURLList:
  180.         case kURLListScroll:
  181.             LClick(localPt, theEvent.modifiers, gURLList);
  182.             break;
  183.         case kFilterMenu:
  184.             GetDItem(gChooserDialogPtr, kFilterMenu, &iType, &filterPopUp, &iRect);
  185.             gFilterValue = GetCtlValue((ControlHandle)filterPopUp);
  186.             GetDItem(gChooserDialogPtr, kInfoField, &iType, &filterPopUp, &iRect);
  187.             RectRgn(infoRgn, &iRect);
  188.             InvalRgn(infoRgn);
  189.         default:
  190.             break;
  191.         }
  192.     
  193.     DisposeRgn(infoRgn);
  194.     SetPort(oldPort);
  195.     }
  196.     
  197. // * *******************************************************************************
  198. // * *******************************************************************************
  199.  
  200. void DoMouseOutsideDialog(EventRecord theEvent, short theItem) {
  201.     short winPart;
  202.     GrafPtr oldPort;
  203.     long menuStuff;
  204.     WindowPtr testWindow;
  205.     
  206.     GetPort(&oldPort);
  207.     SetPort((GrafPtr)gChooserDialogPtr);
  208.     
  209.     winPart = FindWindow(theEvent.where, &testWindow);
  210.     
  211.     switch(winPart) {
  212.         case inMenuBar:
  213.             menuStuff = MenuSelect(theEvent.where);
  214.             if(menuStuff)
  215.                 DoMenu(HiWord(menuStuff), LoWord(menuStuff));
  216.             break;
  217.         case inDrag:
  218.             DragWindow((WindowPtr)gChooserDialogPtr, theEvent.where, &(qd.screenBits.bounds));
  219.             break;
  220.         case inGoAway:
  221.             if(TrackGoAway(gChooserDialogPtr, theEvent.where))
  222.                 gQuitting = TRUE;
  223.             break;
  224.         default:
  225.             break;
  226.         }
  227.     SetPort(oldPort);
  228.     }
  229.  
  230. // * *******************************************************************************
  231. // * *******************************************************************************
  232.  
  233. void DoKey(EventRecord theEvent, short theItem) {
  234.     GrafPtr oldPort;
  235.  
  236.     GetPort(&oldPort);
  237.     SetPort((GrafPtr)gChooserDialogPtr);
  238.     
  239.     
  240.     //Do Stuff...
  241.     
  242.     
  243.     SetPort(oldPort);
  244.     }
  245.     
  246. // * *******************************************************************************
  247. // * *******************************************************************************
  248.  
  249. void DoMenu(short menuID, short menuItem) {
  250.     GrafPtr oldPort;
  251.     Str255 DAName;
  252.     
  253.     GetPort(&oldPort);
  254.     SetPort((GrafPtr)gChooserDialogPtr);
  255.     
  256.     if((menuID == 0) && (menuItem < 0)) {
  257.         GetItem(GetMHandle(128), menuItem, DAName);
  258.         OpenDeskAcc(DAName);
  259.         }
  260.     
  261.     if(menuID == 129) {        //File Menu...
  262.         gQuitting = TRUE;
  263.         }
  264.     
  265.     if(menuID == 130) {        //Edit Menu...
  266.         //Everything is disabled...
  267.         }
  268.  
  269.     HiliteMenu(0);
  270.         
  271.     SetPort(oldPort);
  272.     }
  273.     
  274. // * *******************************************************************************
  275. // * *******************************************************************************
  276.  
  277. OSErr EventLoop(void) {
  278.     DialogPtr myDlg;
  279.     short theItem;
  280.     EventRecord theEvent;
  281.     Boolean handled = FALSE, inDialog = TRUE;
  282.     long menuStuff;
  283.     GrafPtr oldPort;
  284.     
  285.     if(WaitNextEvent(everyEvent, &theEvent, 30, NULL)) {
  286.         GetPort(&oldPort);
  287.         SetPort(gChooserDialogPtr);
  288.         TextFont(0);
  289.         TextSize(0);
  290.         SetPort(oldPort);
  291.         if ((theEvent.what != updateEvt) && (inDialog=IsDialogEvent(&theEvent)))
  292.                 handled = !DialogSelect(&theEvent, &myDlg, &theItem);
  293.         GetPort(&oldPort);
  294.         SetPort(gChooserDialogPtr);
  295.         TextFont(1);
  296.         TextSize(9);
  297.         SetPort(oldPort);
  298.  
  299.         if(handled == FALSE) {
  300.             switch(theEvent.what) {
  301.                 case mouseDown:
  302.                     if(inDialog)
  303.                         DoMouseInDialog(theEvent, theItem);
  304.                     else
  305.                         DoMouseOutsideDialog(theEvent, theItem);
  306.                     break;
  307.                 case keyDown:
  308.                     DoKey(theEvent, theItem);
  309.                     break;
  310.                 case updateEvt:
  311.                     DoUpdate();
  312.                     break;
  313.                 case nullEvent:
  314.                 default:
  315.                     break;
  316.                 }
  317.             }
  318.         else {
  319.             if((theEvent.modifiers & cmdKey)&&(theEvent.what == keyDown))
  320.                 menuStuff = MenuKey(theEvent.message & charCodeMask);
  321.                 if(menuStuff) {
  322.                     HiliteMenu(HiWord(menuStuff));
  323.                     DoMenu(HiWord(menuStuff), LoWord(menuStuff));
  324.                     }
  325.             }
  326.         }
  327.     return noErr;
  328.     }
  329.     
  330. // * *******************************************************************************
  331. // * *******************************************************************************
  332.  
  333. void DoUpdate(void) {
  334.     GrafPtr oldPort;    
  335.     Rect ServiceRect, URLRect, InfoRect;
  336.     short iType;
  337.     Handle iHandle;
  338.  
  339.     GetPort(&oldPort);
  340.     SetPort(gChooserDialogPtr);
  341.     
  342.     GetDItem(gChooserDialogPtr, kServiceList, &iType, &iHandle, &ServiceRect);
  343.     GetDItem(gChooserDialogPtr, kURLList, &iType, &iHandle, &URLRect);
  344.     GetDItem(gChooserDialogPtr, kInfoField, &iType, &iHandle, &InfoRect);
  345.     
  346.     BeginUpdate(gChooserDialogPtr);
  347.  
  348.     UpdtDialog(gChooserDialogPtr, gChooserDialogPtr->visRgn);
  349.     
  350.     InsetRect(&ServiceRect, -1, -1);
  351.     InsetRect(&URLRect, -1, -1);
  352.  
  353.     FrameRect(&ServiceRect);
  354.     FrameRect(&URLRect);
  355.     
  356.     PenPat(&qd.gray);
  357.     FrameRect(&InfoRect);
  358.     PenPat(&qd.black);
  359.     
  360.     LUpdate((*gServiceList)->port->visRgn, gServiceList);
  361.     LUpdate((*gURLList)->port->visRgn, gURLList);
  362.  
  363.     InsetRect(&InfoRect, 2, 2);
  364.     
  365.     NumToString(gFilterValue, gInfo);
  366.     TextBox(&(gInfo[1]), gInfo[0], &InfoRect, 0);
  367.     
  368.     EndUpdate(gChooserDialogPtr);
  369.  
  370.     SetPort(oldPort);
  371.     }
  372.